﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Systems
{
    /// <summary>
    /// This class is used by the texture manager
    /// </summary>
    public class GameTexture
    {
        /// <summary>
        /// The unique ID this texture will have, e.g. "FireParticle"
        /// </summary>
        public string Name;
        /// <summary>
        /// The file path to the texture, e.g. "Images/Particles/Fire"
        /// </summary>
        public Texture2D Image;
    }

    public class GameFont
    {
        /// <summary>
        /// The unique ID this font will have, e.g. "DebugFont"
        /// </summary>
        public string FontName;
        /// <summary>
        /// The file path to the font, e.g. "Fonts/DebugFont"
        /// </summary>
        public SpriteFont Font;
    }
    /// <summary>
    /// The texture manager class allows for easy loading and accessing of textures used
    /// throughout the game
    /// </summary>
    public class TextureManager
    {

        ContentManager theContentManager;
        List<GameTexture> GameTextures;
        List<GameFont> GameFonts;
        public TextureManager(ContentManager contentManager)
        {
            theContentManager = contentManager;
            GameTextures = new List<GameTexture>();
            GameFonts = new List<GameFont>();
        }

        /// <summary>
        /// Adds a texture to the texture manager
        /// </summary>
        /// <param name="TextureName">The name that the texture manager will use as a reference to the texture, e.g. GUN</param>
        /// <param name="TextureFile">The physical file name of the texture, e.g. Background.png or Background</param>
        public void AddTexture(string TextureName, string TextureFile)
        {
            GameTexture newGameTexture = new GameTexture();
            newGameTexture.Name = TextureName;
            newGameTexture.Image = theContentManager.Load<Texture2D>(TextureFile);
            GameTextures.Add(newGameTexture);
        }
        /// <summary>
        /// Adds a font to the texture manager
        /// </summary>
        /// <param name="FontName">The name of the font</param>
        /// <param name="FontFile">The path to the font file</param>
        public void AddFont(string FontName, string FontFile)
        {
            GameFont newGameFont = new GameFont();
            newGameFont.FontName = FontName;
            newGameFont.Font = theContentManager.Load<SpriteFont>(FontFile);
            GameFonts.Add(newGameFont);
        }
        /// <summary>
        /// Returns the specified texture
        /// </summary>
        /// <param name="TextureName">The name of the texture you want to access</param>
        /// <returns>The specified texture as a Texture2D</returns>
        public Texture2D GetTexture(string TextureName)
        {
            foreach (GameTexture currentTexture in GameTextures)
            {
                if (TextureName == currentTexture.Name)
                    return currentTexture.Image;
            }
            return null;
        }

        /// <summary>
        /// Returns a sprite font in the texture manager
        /// </summary>
        /// <param name="FontName">The name of the font</param>
        /// <returns></returns>
        public SpriteFont GetFont(string FontName)
        {
            foreach (GameFont font in GameFonts)
            {
                if (FontName == font.FontName)
                    return font.Font;
            }
            return null;
        }

        /// <summary>
        /// Removes the specified texture from the texture manager
        /// </summary>
        /// <param name="TextureName"></param>
        public void RemoveTexture(string TextureName)
        {
            foreach (GameTexture theTexture in GameTextures)
            {
                if (theTexture.Name == TextureName)
                    GameTextures.Remove(theTexture);
            }
        }

        /// <summary>
        /// Removes the specified font from the texture manager
        /// </summary>
        /// <param name="FontName">The name of the font</param>
        public void RemoveFont(string FontName)
        {
            foreach (GameFont theFont in GameFonts)
            {
                if (theFont.FontName == FontName)
                    GameFonts.Remove(theFont);
            }
        }
        /// <summary>
        /// Clears out all the loaded textures and shuts down the texture manager
        /// </summary>
        public void ShutdownTextureManager()
        {
            for (int i = 0; i < GameTextures.Count; i++)
            {
                GameTextures[i].Name = null;
                GameTextures[i].Image = null;
                GameTextures.Remove(GameTextures[i]);
            }
            GameFonts = null;
        }
    }
}